//
//  HHDetailUserModel.m
//  HHChatKit
//
//  Created by Henry on 2021/9/23.
//

#import "HHDetailUserModel.h"
#import <HHTool/HHTool.h>
#import "HHChatHelper.h"

@implementation HHChatDetailTheme

- (instancetype)init
{
    self = [super init];
    if (self) {
        _imageHeight = 60;
        _nameHeight = 24;
        _spacing = 18;
        _moreHeight = 50;
        _nameFont = [UIFont systemFontOfSize:12];
        _nameColor = [UIColor hh_colorWithLightColor:[UIColor hh_colorWithString:@"#333333"] darkColor:[UIColor hh_colorWithString:@"#DDDDDD"]];
    }
    return self;
}

@end

@interface HHDetailUserModel ()

@property (nonatomic, assign) BOOL more;
@property (nonatomic, assign) NSInteger column;

@property (nonatomic, strong) NSMutableArray <HHSelectUserModel *> *displayDataArray;

@end

@implementation HHDetailUserModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        _maxRow = 4;
        [self initConfig];
    }
    return self;
}

- (instancetype)initWithData:(NSMutableArray *)data {
    return [self initWithData:data maxRow:4];
}

- (instancetype)initWithData:(NSMutableArray *)data maxRow:(NSInteger)maxRow {
    self = [super init];
    if (self) {
        [self initConfig];
        _dataArray = data.mutableCopy;
        _maxRow = maxRow;
    }
    return self;
}

- (void)initConfig {
    _width = [UIScreen mainScreen].bounds.size.width - [HHChatHelper safeAreaInset].left - [HHChatHelper safeAreaInset].right;
    _theme = [[HHChatDetailTheme alloc] init];
}

- (void)setupHeight:(CGFloat)width {
        
    NSMutableArray *data = [NSMutableArray array];
    
    HHChatDetailTheme *theme = self.theme;

    width -= theme.spacing;
    CGFloat itemWidth = theme.imageHeight + theme.spacing;
    NSInteger column = width / itemWidth;
    self.column = column;
    
    CGFloat df = [[NSString stringWithFormat:@"%ld", column] floatValue];
        
    NSInteger total = self.dataArray.count;
    if (self.addPermission) {
        total ++;
    }
    
    if (self.deletePermission) {
        total ++;
    }
    
    NSInteger row = ceil(total / df);

    if (self.maxRow > 0 && row > self.maxRow) {
        row = self.maxRow;
        self.more = YES;
        
        NSInteger ototal = row * column;
        
        if (self.addPermission && self.deletePermission) {
            data = [self.dataArray subarrayWithRange:NSMakeRange(0, ototal-2)].mutableCopy;
            [data addObject:[HHSelectUserModel modelWithAdd]];
            [data addObject:[HHSelectUserModel modelWithDelete]];
        } else if (self.addPermission) {
            data = [self.dataArray subarrayWithRange:NSMakeRange(0, ototal-1)].mutableCopy;
            [data addObject:[HHSelectUserModel modelWithAdd]];
        } else if (self.deletePermission) {
            data = [self.dataArray subarrayWithRange:NSMakeRange(0, ototal-1)].mutableCopy;
            [data addObject:[HHSelectUserModel modelWithDelete]];
        }
    
    } else {
        data = self.dataArray.mutableCopy;
        
        if (self.addPermission) {
            [data addObject:[HHSelectUserModel modelWithAdd]];
        }
        if (self.deletePermission) {
            [data addObject:[HHSelectUserModel modelWithDelete]];
        }
    }
    
    self.displayDataArray = data.mutableCopy;
    
    CGFloat itemHeight = theme.imageHeight + theme.nameHeight + theme.spacing;
    CGFloat height = itemHeight * row + theme.spacing;
    
    if (self.more) {
        height += theme.moreHeight;
    }
    
    self.cellHeight = height;
}

@end
